var express = require('express')
var router = express.Router()
const alipaySdk = require('./untils/alipayuntils.js')
const AlipayFormData = require('alipay-sdk/lib/form').default
// const axios = require('axios')

const cors = require('cors') //解决跨域请求问题
router.use(cors())
router.use(express.json());
router.use(express.urlencoded({ extended: true }))
 
/* GET home page. */
router.get('/', function(req, res) {  //路由测试
  res.send('index')
})
 
router.post('/api/payment', function(req, res) { //支付宝数据封装
  const orderId = req.body.orderId;
  const price = req.body.price;
  const userid = req.body.userid
//   console.log(orderId)
// console.log(req.body)
  // 对接支付宝
  const formData = new AlipayFormData();
 
  formData.setMethod('get');
 
  
 formData.addField('notifyUrl','http://baidu.com')
// 商单信息
  formData.addField('bizContent', {
    outTradeNo: orderId,
    out_trade_no: "21245956656"+Math.random(1,400)+"1128",
    product_code: "FAST_INSTANT_TRADE_PAY",
    subject: "会员",
    body: "商品详情",
    timeout_express: "90m",
    total_amount: price
 
  });
  formData.addField('returnUrl','http://192.168.2.27:8080/ford/#/successpay'); //回调地址
  let result = alipaySdk.exec(
    'alipay.trade.page.pay',
    { userid: userid },
    { formData: formData }
 
  );
 
  result.then((resp) => {
    res.send({
      success: 'true',
      code: 200,
      result: resp
    });
  });

  
});
module.exports = router;